<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib uri="http://www.bjs-tech.com/taglib/dataDict" prefix="dataDict" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<jsp:useBean id="launchtime" class="java.util.Date" />
<jsp:setProperty name="launchtime" property="time" value="${umbrellaHardwareMachine.launchtime}"/>

<div id="updateDialog" class="container-fluid">
    <div class="row">
        <form id="updateForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="name">
                    <span class="help-inline"><font color="red">*</font> </span>
                    名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="name" name="name" placeholder="名称" value="${umbrellaHardwareMachine.name}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="code">
                    <span class="help-inline"><font color="red">*</font> </span>
                    机器码：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="code" name="code" placeholder="机器码" value="${umbrellaHardwareMachine.code}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="pathcount">
                    <span class="help-inline"><font color="red">*</font> </span>
                    伞道数：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="pathcount" name="pathcount" placeholder="伞道数" maxlength="2" value="${umbrellaHardwareMachine.pathcount}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="capacity">
                    <span class="help-inline"><font color="red">*</font> </span>
                    额定容量(把)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="capacity" name="capacity" placeholder="额定容量" value="${umbrellaHardwareMachine.capacity}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="type">
                    <span class="help-inline"><font color="red">*</font> </span>
                    类型：</label>
                <div class="col-sm-5">
                    <select id="type" name="type" class="form-control" placeholder="类型">
                        <c:forEach var="machineType" items="${machineTypeList}">
                            <option value="${machineType.dicValue}" <c:if test="${umbrellaHardwareMachine.type==machineType.dicValue}">selected="selected"</c:if>>${machineType.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="status">
                    <span class="help-inline"><font color="red">*</font> </span>
                    状态：</label>
                <div class="col-sm-5">
                    <select id="status" name="status" class="form-control" placeholder="状态">
                        <c:forEach var="upmsDict" items="${statusList}">
                            <option value="${upmsDict.dicValue}" <c:if test="${umbrellaHardwareMachine.status==upmsDict.dicValue}">selected="selected"</c:if>>${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="rentRuleId">
                    <span class="help-inline"><font color="red">*</font> </span>
                    租赁规则：</label>
                <div class="col-sm-5">
                    <select id="rentRuleId" name="rentRuleId" class="form-control" placeholder="租赁规则">
                        <c:forEach var="ruleRent" items="${ruleRentList}">
                            <option value="${ruleRent.id}" <c:if test="${umbrellaHardwareMachine.rentRuleId==ruleRent.id}">selected="selected"</c:if>>${ruleRent.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="ruleDepositId">
                    <span class="help-inline"><font color="red">*</font> </span>
                    预付金规则：</label>
                <div class="col-sm-5">
                    <select id="ruleDepositId" name="ruleDepositId" class="form-control" >
                        <c:forEach var="deposit" items="${depositList}">
                            <option value="${deposit.id}" <c:if test="${umbrellaHardwareMachine.ruleDepositId==deposit.id}">selected="selected"</c:if>>${deposit.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="launchtime">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放时间：</label>
                <div class="col-sm-5">
                        <input type="text" id="launchtime" class="form-control" data-date-format="yyyy-mm-dd" placeholder="投放时间" value="<fmt:formatDate value="${launchtime}" pattern="yyyy-MM-dd" />">
                        <input type="text" hidden name="launchtime" value="${umbrellaHardwareMachine.launchtime}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="country">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放国家：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="country" name="country" placeholder="投放国家" value="${umbrellaHardwareMachine.country}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="province">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放省份：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="province" name="province" placeholder="投放省份" value="${umbrellaHardwareMachine.province}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="city">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放地市：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="city" name="city" placeholder="投放地市" value="${umbrellaHardwareMachine.city}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="address">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放详细地址：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="address" name="address" placeholder="投放详细地址" value="${umbrellaHardwareMachine.address}"/>
                </div>
            </div>
            <%--<div class="form-group">--%>
                <%--<label class="col-sm-4 control-label" for="city">--%>
                    <%--<span class="help-inline"><font color="red">*</font> </span>--%>
                    <%--伞机状态：</label>--%>
                <%--<div class="col-sm-5">--%>
                    <%--<dataDict2Select id="status" name="status" class="form-control" placeholder="伞机状态" typeCode="UM_MACHINE_STATUS" value="${umbrellaHardwareMachine.status}"></dataDict2Select>--%>
                <%--</div>--%>
            <%--</div>--%>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="longitude">
                    <span class="help-inline"><font color="red">*</font> </span>
                    经度：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="longitude" name="longitude" placeholder="经度" value="${umbrellaHardwareMachine.longitude}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="latitude">
                    <span class="help-inline"><font color="red">*</font> </span>
                    纬度：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="latitude" name="latitude" placeholder="纬度" value="${umbrellaHardwareMachine.latitude}"/>
                </div>
            </div>
            <div class="form-group">
                <%--<label class="col-sm-4 control-label" for="description">点击地图获取坐标</label>--%>
                <%--<div class="col-sm-5">--%>
                    <%--<input type="text" class="form-control" id="searchmap" placeholder="输入关键字搜索地图位置。" />--%>
                <%--</div>--%>
                <%--<div class="col-sm-12   ">--%>
                    <%--<div id="l-map"></div>--%>
                <%--</div>--%>
                    <div class="col-sm-8 col-sm-offset-4">
                        <button type="button" class="btn btn-primary" onclick="openMap();">打开地图获取坐标</button>
                    </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="description">描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="description" name="description" placeholder="描述" value="${umbrellaHardwareMachine.description}"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="updateDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
   /* // 百度地图API功能
    var map = new BMap.Map("l-map", {enableMapClick:false});            // 创建Map实例
    var point = {lng:${umbrellaHardwareMachine.longitude},lat:${umbrellaHardwareMachine.latitude}};
    $.MapConvert.Convert_GCJ02_To_BD09(point);
    console.log(point);
    map.centerAndZoom(new BMap.Point( point.lng,point.lat), 11);
    var local = new BMap.LocalSearch(map, {
        renderOptions: {map: map, panel: "r-result"}
    });
    local.search("");
    $("#searchmap").change(function(){
        local.search($(this).val());
    });
    setTimeout(function(){
        map.setZoom(14);
    }, 2000);  //2秒后放大到14级
    map.enableScrollWheelZoom(true);
    //单击获取点击的经纬度
    map.addEventListener("click",function(e){
        var point = e.point;
        $.MapConvert.Convert_BD09_To_GCJ02(point);
        $("#latitude").val(point.lat);
        $("#longitude").val(point.lng);
    });     */
</script>
    <script>
        function validateForm(){
            return $( "#updateForm" ).validate( {
                rules: {
                    code: {
                        required: true,
                        maxlength: 50
                    },
                    pathcount: {
                        required: true,
                        maxlength: 2
                    },
                    name: {
                        required: true,
                        maxlength: 50
                    },
                    capacity: {
                        required: true,
                    },
                    type: {
                        required: true,
                    },
                    status: {
                        required: true,
                    },
                    launchtime: {
                    },
                    country: {
                    },
                    province: {
                    },
                    city: {
                    },
                    address: {
                    },
                    longitude: {
                        required: true,
                    },
                    latitude: {
                        required: true,
                    },
                    description: {
                        maxlength: 500
                    }
                },
                messages: {
                    code: {
                        required: "此项为必填项。",
                        maxlength: "长度超出50个字符"
                    },
                    pathcount: {
                        required: "此项为必填项。",
                        maxlength: "长度超出2个数字"
                    },
                    name: {
                        required: "此项为必填项。",
                        maxlength: "长度超出50个字符"
                    },
                    capacity: {
                        required: "此项为必填项。",
                    },
                    type: {
                        required: "此项为必填项。",
                    },
                    status: {
                        required: "此项为必填项。",
                    },
                    launchtime: {
                    },
                    country: {
                    },
                    province: {
                    },
                    city: {
                    },
                    address: {
                    },
                    longitude: {
                        required: "此项为必填项。",
                    },
                    latitude: {
                        required: "此项为必填项。",
                    },
                    description: {
                        maxlength: "描述长度超出500个字符"
                    }
                },
                errorElement: "em",
                errorPlacement: function ( error, element ) {
                    // Add the `help-block` class to the error element
                    error.addClass( "help-block" );

                    if ( element.prop( "type" ) === "checkbox" ) {
                        error.insertAfter( element.parent( "label" ) );
                    } else {
                        error.insertAfter( element );
                    }
                },
                highlight: function ( element, errorClass, validClass ) {
                    $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
                },
                unhighlight: function (element, errorClass, validClass) {
                    $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
                }
            } );
        }
    //注册表单验证
    $(validateForm());
        $(function(){
            $('#launchtime').datepicker({
//            format: 'yyyy-mm-dd'
            });
        });

    function createSubmit() {
        if(validateForm().form()){
            if($("#launchtime").val())
                $("input[name='launchtime']").val(getLongFromatDateStr($("#launchtime").val()));
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/machine/update/${umbrellaHardwareMachine.id}',
                data: $('#updateForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        updateDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>